/**
 * 
 */
var AQIChinaMap = (function() {
	// 初始化
	function AQIChinaMap(options) {		
		this.datas={};
		this.ol2dlist={};
		this.id=options.id;
		
		this.map = options.map;
		this.map2d=this.map.map2d;
		
		this.init();		
		
	}
	//
	// 初始化
	AQIChinaMap.prototype.init = function(options) {
		var that = this;		 

		
	}
	AQIChinaMap.prototype.loaddata = function(options) {
		var that = this;		 

		var promise = $.get(options.url);
		
		promise.done(function(data){
			that.datas=data;
			//console.info(data);
			that.loadmark();
			
			function changepop(e){
				  $(".text-lvl-1").css("color","#FFF000");
			}
			 // 设置点击事件模板
            var template = "<p>正在加载... ...</p>"; // {{station}}:<code>{{station}}{{station}}</code>
            //console.info(that.id);
            that.map2d.clicks({
                "title": "utime",
                "layerid": that.id,
                "template": template,
                "script":changepop,
                "url": "views/aqidatapop.html",//"?layerid=" + that.id + "&id={{idx}}&tm=" + new Date().getTime()
            });
            
		}); 
		     
	}
	
	AQIChinaMap.prototype.loadmark = function(options) {
		var that = this;	
		
        var customStyleFunction = function (feature, resolution) {
            // 根据空气级别，设定不同颜色
            var radius=7;
            var forecolor="#FFFFFF";
            var backcolor="#666666";
            var resokey=4892.96981025128;
            var myval="-";
            //console.info(resolution);
            if(feature.data.value){
            	myval=feature.data.value;
            }
            
            if(myval==="-"){
            	backcolor="#666666";
            }else {
            	 
            	if(myval<=50){
            		radius=7;
            		backcolor="#009966";
            	}else if(myval<=100){
            		radius=8;
            		forecolor="#000000";
            		backcolor="#FFDE33";
            	}else if(myval<=150){
            		radius=9;
            		forecolor="#000000";
            		backcolor="#FF9933";
            	}else if(myval<=200){
            		radius=10;
            		backcolor="#CC0033";
            	}else if(myval<=300){
            		radius=11;
            		backcolor="#660099";
            	}else{
            		radius=11;
            		backcolor="#7E0023";
            	}            	
            }
            
            var styleimg = new ol.style.Style({ 
                
                image: new ol.style.Circle({
                    radius: radius,
                    fill: new ol.style.Fill({
                      color: backcolor,
                    })
                  }),
	          
            });
            var styleimgmax = new ol.style.Style({ 	                
	                image: new ol.style.Circle({
	                    radius: radius+1,
	                    fill: new ol.style.Fill({
	                      color: "#eeeeee",
	                    })
	                  }),
		           
	            });
            var styletext = new ol.style.Style({
                text: new ol.style.Text({
                    textAlign: "center",
                    textBaseline: "middle",
                    font: '10px Normal Arial',
                    text: feature.data.value+"", //that.mapoptions.key + feature.get('features')[0].data[that.mapoptions.value] + that.mapoptions.unit,//,// //feature.data.station,
                    fill: new ol.style.Fill({
                        color: forecolor, //// 
                    }),
//                    stroke: new ol.style.Stroke({
//                        color: '#000000',
//                        width: 1
//                    }),
 
                    offsetX: 0,
                    offsetY: 1,
                    rotation: 0
                })
            }); 
            if(resolution>resokey){
            	 style = [styleimgmax,styleimg];
            }else{
            	 style = [styleimgmax,styleimg, styletext];
            }
           
 
            //  console.info(style);
            return style;
        };
        //console.info(that.datas);
        $(that.datas.pointsJsonArray).each(function (i, val) {
            // console.info(val);
            var geom = new ol.geom.Point(ol.proj.transform([val.xvalue, val.yvalue], 'EPSG:4326','EPSG:3857'));
            var feature = new ol.Feature(geom);
            feature.data = val;
            feature.layerid = that.id;
            // feature.setStyle(iconStyle); 
            that.ol2dlist[val.pointid+""] = feature;
 
        });
 
        // Source and vector layer
        //console.info(that.ol2dlist);
        var vectorSource = new ol.source.Vector({
            features: _.values(that.ol2dlist)
        });
        var imageSource= new ol.source.ImageVector({
            source:vectorSource,
            style:customStyleFunction
        });
        
        var tmpLayer = that.map2d.getlayerbyid({"layerid":that.id});
        if (tmpLayer == null) {
            tmpLayer = new ol.layer.Image({
                id: that.id,
                opacity: 0.90,
                maxzoom: 1224,
                minzoom: 0.0001,
                //source: vectorSource,
                //style: customStyleFunction
            });
            that.map2d.olmap.addLayer(tmpLayer);
        }  
        
        //imageSource.setStyle(customStyleFunction);

        //tmpLayer.setSource(null);
        tmpLayer.setSource(imageSource);
          
	}
	 
	// 返回
	return AQIChinaMap;
})();
 